<template>
  <div class="list"
       ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper"
               v-for="item of hot"
               :key="item.id"
               @click="handleCityClick(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area"
           v-for="(item,key) of cities"
           :key="key"
           :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom"
               v-for="innerItem of item"
               :key="innerItem.id"
               @click="handleCityClick(innerItem.name)">{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  components: {
    Bscroll
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick (city) {
      /* 如果是简单数据，就不需要用dispath调用actions；可以直接跳过,使用commit方法调用mutations */
      // this.$store.dispatch('changeCity', city)
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeCity'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper, {
      // better-scroll 默认会阻止浏览器的原生 click 事件。当设置为 true，better-scroll 会派发一个 click 事件，我们会给派发的 event 参数加一个私有属性 _constructed，值为 true。
      click: true,
      tap: true
    })
  },
  watch: {
    letter () {
      console.log(this.letter)
      if (this.letter) {
        // 获取dom元素，让scroll跳转到对应位置
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang='scss' scoped>
@import '~@sass/varibles/_color.scss';
@import '~@sass/mixins/_global.scss';
.title {
  line-height: 0.44rem;
  background: #eee;
  padding-left: 0.2rem;
  color: #666;
  font-size: 0.26rem;
}
.border-topbottom,
.border-bottom {
  &:before,
  &:after {
    border-color: #ccc;
  }
}
.list {
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}
.button-list {
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
  @include g-clearfix;
}
.button-wrapper {
  float: left;
  width: 33.33%;
}
.button {
  line-height: 1.5;
  margin: 0.1rem;
  text-align: center;
  border: 0.02rem solid #ccc;
  border-radius: 0.06rem;
}
.item {
  line-height: 3;
  color: #666;
  padding-left: 0.2rem;
}
.button,
.item {
  &:active {
    background-color: rgba(0, 0, 0, 0.3);
  }
}
.item.border-bottom:last-child {
  &:after,
  &:before {
    border-color: transparent;
  }
}
</style>
